package com.itheima.service.imp;

import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class SetmealServiceImp implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;


    /** author: mashuning
     * 查询所有套餐
     * @return
     */
    public List<Setmeal> getSetmealAll(){
        return setmealMapper.getSetmealAll();
    }

    /** author: mashuning
     * 根据套餐id查询套餐
     * @param id
     * @return
     */
    public Setmeal findById(Integer id){
        //1.先查出t_setmeal
        Setmeal setmeal = setmealMapper.getSetmealById(id);
        if(setmeal == null){
            return null;
        }
        //2.根据t_setmeal.id查询t_checkgroup_checkitem表
        List<CheckGroup> checkGroups = setmealMapper.getCheckGroupsBySetmealId(id);
        if (checkGroups != null && checkGroups.size() > 0){
            List<Integer> checkGroupIds = new ArrayList<>();
            for (CheckGroup checkGroup : checkGroups) {
                checkGroupIds.add(checkGroup.getId());
            }
            log.info("groupIds是：{}",checkGroupIds);

            //3.根据t_checkgroup_checkitem表查询t_checkitem表
            List<CheckItem>  checkItems= setmealMapper.getCheckItemsByCheckGroupIds(checkGroupIds);
            for (CheckGroup checkGroup : checkGroups) {
                for (CheckItem checkItem : checkItems){
                    log.info("grpId:{} -- cheGrpId:{}",checkGroup.getId(),checkItem.getCheckgroupId());

                    if(checkGroup.getId() == checkItem.getCheckgroupId()){

                        checkGroup.getCheckItems().add(checkItem);

                    }
                }
            }

            setmeal.setCheckGroups(checkGroups);
        }


        return setmeal;
    }


    //通用的方法，用于生成静态页面
    public void generteHtml(String templateName, String htmlPageName, Map map){
        Configuration configuration = freeMarkerConfigurer.getConfiguration();//获得配置对象
        Writer out = null;
        try {
            Template template = configuration.getTemplate(templateName);

            String absolutePath = new File("").getAbsolutePath() +
                    "/bxg-health-mobile/src/main/resources/static/";
            //构造输出流
            out = new FileWriter(new File(  absolutePath + htmlPageName));


            //输出文件
            template.process(map,out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //生成套餐详情静态页面（可能有多个）
    public void generateMobileSetmealDetailHtml(List<Setmeal> list){
        for (Setmeal setmeal : list) {
            Map map = new HashMap();
            map.put("setmeal",findById(setmeal.getId()));
            generteHtml("setmeal_detail.ftl","setmeal_detail_" + setmeal.getId() + ".html",map);
        }
    }



}
